package com.raincat.tools.nettools;

import android.content.Context;

import androidx.annotation.NonNull;

import java.lang.ref.WeakReference;
import java.util.HashMap;

/**
 * <pre>
 *     author : RainCat
 *     org    : Shenzhen JingYu Network Technology Co., Ltd.
 *     e-mail : nining377@gmail.com
 *     time   : 2020/03/23
 *     desc   : 下载配置
 *     version: 1.0
 * </pre>
 */
public class DownloadConfig {
    static final String DOWNLOAD = "DOWNLOAD";
    private Request request;

    public DownloadConfig() {
        request = new Request();
        request.requestMode = DOWNLOAD;
        request.cookie = NetConfig.cookie;
        request.reTry = NetConfig.reTry;
        request.connectTimeout = NetConfig.connectTimeout;
        request.readTimeout = NetConfig.readTimeout;
    }

    public DownloadConfig doDownLoad(@NonNull String url, int notificationResId, boolean isShowDownDialog, boolean isDialogCancelable) {
        request.notificationResId = notificationResId;
        request.isShowDownDialog = isShowDownDialog;
        request.isDialogCancelable = isDialogCancelable;
        request.url = url;
        return this;
    }

    /**
     * 请求头
     *
     * @param headers 请求头
     * @return 返回值
     */
    public DownloadConfig headers(@NonNull HashMap<String, Object> headers) {
        request.headers = headers;
        return this;
    }

    /**
     * 是否显示加载框
     *
     * @param dialogMessage 加载框信息
     * @return 返回值
     */
    public DownloadConfig showLoadingDialog(@NonNull String dialogMessage) {
        request.dialogMessage = dialogMessage;
        return this;
    }

    public void start(Context context, NetCallBack back) {
        if (request.url == null || request.url.length() == 0)
            throw new IllegalArgumentException("url can't be null");
        request.context = new WeakReference<>(context);
        request.back = back;
        ConnectDownload connectDownload = new ConnectDownload();
        connectDownload.init(request);
    }
}
